package dj.action;

import java.util.ArrayList;
import java.util.List;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ModelDriven;

import dj.Constant;
import dj.action.base.BaseAction;
import dj.model.SysRoles;
import dj.pageModel.PsysRoles;
import dj.pageModel.Puser;
import dj.service.SysRoleService;

@Action("sysRoleAction")
public class SysRoleAction extends BaseAction implements ModelDriven<PsysRoles> {

	private PsysRoles pr = new PsysRoles();

	private SysRoleService sysRoleService;

	public PsysRoles getModel() {
		return pr;
	}

	public SysRoleService getSysRoleService() {
		return sysRoleService;
	}

	@Autowired
	public void setSysRoleService(SysRoleService sysRoleService) {
		this.sysRoleService = sysRoleService;
	}

	public void getSysRoles() {
		Puser puser = (Puser) ServletActionContext.getRequest().getSession().getAttribute(Constant.USER_SESSION);
		List<SysRoles> srs = new ArrayList<SysRoles>();
		List<PsysRoles> prs = new ArrayList<PsysRoles>();
		if (null != puser) {
			srs = sysRoleService.getSysRoleByMemberid(puser.getId(), Constant.USERTYPE_USER);
			if(null != srs && srs.size() > 0) {
				for(SysRoles sr : srs) {
					PsysRoles psr = new PsysRoles();
					BeanUtils.copyProperties(sr, psr);
					prs.add(psr);
				}
			}
		}

		super.writeToJson(prs);
	}
}
